package cn.chrisn.chatapi.interfaces;

import cn.chrisn.chatapi.domain.security.model.vo.Result;
import cn.chrisn.chatapi.domain.security.service.JwtUtils;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@Slf4j
public class ApiAccessController {

    @GetMapping("/login")
    public Result verity(String userName, String password){
        if (!userName.equals("chrisn")||!password.equals("123"))
        {
            return Result.error("密码错误");
        }
        HashMap<String, Object> claims = new HashMap<>();
        claims.put("userName","chrisn");
        String jwt = JwtUtils.generateJwt(claims);
        return Result.success(jwt);
    }
    @GetMapping("/verify")
    public ResponseEntity<String> verity(String token){
        log.info("验证token：{}",token);
        Claims claims = JwtUtils.parseJWT(token);
         String userName =(String) claims.get("userName");
        if ("chrisn".equals(userName)){
            return ResponseEntity.status(HttpStatus.OK).build();
        }else {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }
    }
    @GetMapping("/success")
    public String success(){
        return "hello，chrisn";
    }
}
